//Copyright 2012 Trevin Liberty
//GNU GPL v3
//Author: Trevin Liberty

#include "Doohickey.h"

int Doohickey::nextValidID = 0;
bitmapImage* Doohickey::healthBar = NULL;

Doohickey::Doohickey(int maxHealth, Map* map, Point<int> imageBound) {
	pos.x = 0;
	pos.y = 0;
	maxHealthPoints = maxHealth;
	healthPoints = maxHealth;

	id = nextValidID;
	nextValidID++;

	this->map = map;
	this->imageBound = imageBound;

	selected = false;
	remove = false;
	entity = false;

	if(healthBar == NULL) {
		healthBar = new bitmapImage();
		healthBar->image = al_load_bitmap("rsc/GUI/Health.tga");
		healthBar->sizeX = al_get_bitmap_width(healthBar->image);
		healthBar->sizeY = al_get_bitmap_height(healthBar->image);
	}
}

void Doohickey::drawHealth(Point<float> cameraPos, Point<int> offset, int maxLength) {
	float per = healthPoints;
	per = (per / maxHealthPoints)*100.0;
	//healthBar length in pixels/100 * percentage of health left
	float length = (maxLength/100.0)*per;

	Point<float> p = pos;
	p.x = p.x - cameraPos.x + offset.x;
	p.y = p.y - cameraPos.y + offset.y;

	int r = 0;
	int g = 255;

	if(per - 50 <= 0) {
		g = 255*(per/50);
		r = 255;
	}
	else if(per - 50 > 0)
		r = 255 - 255*((per-50)/50);

	al_draw_filled_rectangle(p.x, p.y, p.x + length, p.y + 4, al_map_rgb(r, g, 0));
	al_draw_bitmap(healthBar->image, p.x - 2, p.y - 2, 0);
}